/**
 * @desc 填写的订单
 */

import { setState } from './index'
import { query, mutation } from '@/utils/http'

function apiGetTravelDetail(token, id) {
  const str = `
  ($id:Long!){
    travelRouteDetail(id:$id){
      id
      name
      picture
      labelList
      isVisa
      costExplain
      buyInfo
      routeIntros{
        id
        type
        content
      }
      schedules{
        id
        isBreakfast
        isLunch
        isSupper
        scheduleIntros{
          id
          type
          content
        }
        schedulePlaces{
          id
          name
          traffic
          number
          placeViews{
            id
            name
            number
            intro
          }
        }
      }
      singlePersonPrice
      twoPeoplePrice
      fourPeoplePrice
      sixPeoplePrice
      eightPeoplePrice
      singleRoomPrice
      routePrices{
        id
        thatDate
        singlePersonPrice
        twoPeoplePrice
        fourPeoplePrice
        sixPeoplePrice
        eightPeoplePrice
        singleRoomPrice
      }
    }
  }
  `
  return query(token, str, { id })
}

function apiSendOrder(token, params) {
  const str = `
  ($request:TravelRouteOrderCreateInput!){
    createTravelRouteOrder(request:$request)
  }
  `
  return mutation(token, str, { request: params })
}

function apiPayOrder(token, params) {
  const str = `
  ($request:TravelRouteOrderPayInput!){
    payTravelRouteOrder(request:$request){
      timeStamp
      nonceStr
      payPackage
      paySign
      signType
    }
  }
  `
  return mutation(token, str, { request: params })
}

const state = {
  currTravelDetail: {},
  currDatePrice: {}, // 当前日期的套餐价格

  // form
  routeId: '',
  travelDate: '',
  singlePersonGroup: 0,
  twoPeopleGroup: 0,
  fourPeopleGroup: 0,
  sixPeopleGroup: 0,
  eightPeopleGroup: 0,
  roomQuantity: 0,
  orderAmount: 0,
  userTravelerId: ''
}

const getters = {
  checkedPackages(state) {
    if (!state.currDatePrice.id) {
      return []
    }
    const arr = []
    if (state.singlePersonGroup) {
      arr.push({
        name: '单人套餐',
        quantity: 1,
        price: state.currDatePrice.singlePersonPrice
      })
    }
    if (state.twoPeopleGroup) {
      arr.push({
        name: '双人套餐',
        quantity: 1,
        price: state.currDatePrice.twoPeoplePrice * 2
      })
    }
    if (state.fourPeopleGroup) {
      arr.push({
        name: '四人套餐',
        quantity: 1,
        price: state.currDatePrice.fourPeoplePrice * 4
      })
    }
    if (state.sixPeopleGroup) {
      arr.push({
        name: '六人套餐',
        quantity: 1,
        price: state.currDatePrice.sixPeoplePrice * 6
      })
    }
    if (state.eightPeopleGroup) {
      arr.push({
        name: '八人套餐',
        quantity: 1,
        price: state.currDatePrice.eightPeoplePrice * 8
      })
    }
    if (state.roomQuantity) {
      arr.push({
        name: '单房',
        quantity: state.roomQuantity,
        price: state.currDatePrice.singleRoomPrice * state.roomQuantity
      })
    }
    return arr
  }
}

const mutations = { setState }

const actions = {
  async getTravelDetail({ rootState, commit }, id) {
    // test
    const t = new Date().getTime()

    const token = rootState.Client.token
    return new Promise((resolve, reject) => {
      apiGetTravelDetail(token, id).then(res => {
        if (res.data.data && res.data.data.travelRouteDetail) {
          // 获取详情时,把当前的路线id作为订单下单的'routeId'
          commit('setState', {
            currTravelDetail: res.data.data.travelRouteDetail,
            routeId: res.data.data.travelRouteDetail.id
          })
          resolve()
        } else {
          reject()
        }
      })
    })
  },

  sendOrder({ rootState }, params) {
    const token = rootState.Client.token
    return new Promise((resolve, reject) => {
      apiSendOrder(token, params).then(res => {
        if (res.data.data && res.data.data.createTravelRouteOrder) {
          const orderNumber = res.data.data.createTravelRouteOrder
          apiPayOrder(token, { orderNumber }).then(res => {
            if (res.data.data && res.data.data.payTravelRouteOrder) {
              const pay = res.data.data.payTravelRouteOrder
              wx.requestPayment({
                ...pay,
                timeStamp: pay.timeStamp + '',
                package: pay.payPackage,
                complete: () => resolve()
              })
            }
          })
        } else if (res.data.errors) {
          reject(res.data.errors[0].message)
        }
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
